<template>
  <MessageOverride />
  <div class="app-container">
  <router-view v-slot="{ Component, route }">
    <transition name="fade" mode="out-in">
        <suspense>
          <template #default>
      <keep-alive :include="cachedViews">
        <component :is="Component" :key="route.path" />
      </keep-alive>
          </template>
          <template #fallback>
            <div class="page-loading">
              <el-icon class="loading-icon"><Loading /></el-icon>
              <span>页面加载中...</span>
            </div>
          </template>
        </suspense>
    </transition>
  </router-view>
  </div>
</template>

<script setup>
import MessageOverride from './components/MessageOverride.vue';
import { computed, onMounted, onUnmounted, getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const router = useRouter();
const route = useRoute();

// 计算需要缓存的组件名称列表
const cachedViews = computed(() => {
  // 从路由配置中获取所有keepAlive为true的路由的组件名称
  return router.getRoutes()
    .filter(route => route.meta?.keepAlive)
    .map(route => route.name)
    .filter(Boolean);
});

// 预加载常用页面组件
onMounted(() => {
  // 预加载首页和其他关键页面
  import('./views/home/Home.vue');
  import('./views/category/Category.vue');
  import('./views/search/Search.vue');
  
  // 添加页面可见性监听，避免页面切换后被挂起
  document.addEventListener('visibilitychange', handleVisibilityChange);
});

onUnmounted(() => {
  document.removeEventListener('visibilitychange', handleVisibilityChange);
});

// 处理页面可见性变化
function handleVisibilityChange() {
  if (document.visibilityState === 'visible') {
    // 页面变为可见时，刷新当前组件数据
    const currentInstance = getCurrentInstance();
    if (currentInstance && currentInstance.proxy && currentInstance.proxy.$forceUpdate) {
      currentInstance.proxy.$forceUpdate();
    }
  }
}
</script>

<style>
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  touch-action: manipulation;
  -webkit-overflow-scrolling: touch;
  -webkit-tap-highlight-color: transparent;
  /* 增强阻止缩放的样式 */
  touch-action: pan-x pan-y; 
  -ms-touch-action: pan-x pan-y;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* 防止双击缩放 */
* {
  touch-action: manipulation;
}

.app-container {
  width: 100%;
  height: 100%;
  position: relative;
}

#app {
  width: 100%;
  height: 100%;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  /* 固定尺寸和缩放 */
  max-width: 100vw;
  position: relative;
  overflow-x: hidden;
}

/* 页面加载中样式 */
.page-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  color: #909399;
}

.loading-icon {
  font-size: 30px;
  animation: loading-rotate 2s linear infinite;
  margin-bottom: 15px;
}

@keyframes loading-rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 确保router-view区域始终有内容，防止空白闪烁 */
.router-view-container {
  min-height: 100vh;
  position: relative;
  background-color: #f5f7fa;
}

/* 页面切换淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.5;
  background-color: #f5f7fa;
  /* 允许页面滚动，避免固定导致的问题 */
  height: 100%;
  overflow-y: auto;
}

/* 支持内容滚动的同时阻止整页缩放 */
.content-scrollable {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.el-menu.el-menu--horizontal {
  border-bottom: none;
}

.el-button+.el-button {
  margin-left: 10px;
}

.el-form-item.is-required .el-form-item__label:before {
  margin-right: 2px;
}

/* 苹果风格消息提示强制样式 */
.el-message {
  min-width: 400px !important;
  max-width: 600px !important;
  padding: 18px 24px !important;
  border: none !important;
  border-radius: 16px !important;
  background-color: rgba(0, 0, 0, 0.8) !important;
  backdrop-filter: blur(12px) !important;
  -webkit-backdrop-filter: blur(12px) !important;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3) !important;
  margin-top: 20px !important;
  color: white !important;
}

.el-message .el-message__content {
  font-size: 17px !important;
  line-height: 1.5 !important;
  font-weight: 400 !important;
  color: white !important;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;
}

.el-message .el-message__icon {
  font-size: 22px !important;
  margin-right: 16px !important;
  color: white !important;
}

.el-message--success .el-message__icon {
  color: #66d763 !important;
}

.el-message--warning .el-message__icon {
  color: #ffb840 !important;
}

.el-message--error .el-message__icon {
  color: #ff453a !important;
}

.el-message--info .el-message__icon {
  color: #32ade6 !important;
}

/* 媒体查询确保移动设备上的正确显示 */
@media (max-width: 768px) {
  .el-message {
    min-width: 90% !important;
    max-width: 95% !important;
    margin: 10px auto !important;
  }
}
</style>
